package com.wyf.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Verification;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wyf
 * @date 2022年07月17日 18:26
 */
public class JWTUtils {

    private static final String SECRET = "wangyifanliuyibo";
    private static final Long EXPIRED = 600000l;

    /**
     * 创建一个token
     * @param map   想要在token存储用户的数据
     * @return
     */
    public String createToken(Map<String,Object> map) {
        JWTCreator.Builder builder = JWT.create();
        String token = builder.withPayload(map)
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRED))
                .sign(Algorithm.HMAC256(SECRET));
        return token;
    }

    public void verifyToken(String token) {
        Verification require = JWT.require(Algorithm.HMAC256(SECRET));

            DecodedJWT verify = require.build().verify(token);

    }

}
